package top.frsf.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;
import top.frsf.reggie.common.R;
import top.frsf.reggie.entity.Employee;
import top.frsf.reggie.service.EmployeeService;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;

/**
 * @Author FRSF
 * @Date 2023/1/9 23:41
 * @Description 员工控制层
 */
@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;


    /**
     * @Author: FRSF
     * @Date: 2023/1/10 0:05
     * @Description 用户登录
     */
    @PostMapping("/login")
    public R<Employee> login(HttpServletRequest request, @RequestBody Employee employee) {
        // 1.密码md5加密处理
        String password = employee.getPassword();
        password = DigestUtils.md5DigestAsHex(password.getBytes());

        // 2.根据用户名查询database
        LambdaQueryWrapper<Employee> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Employee::getUsername, employee.getUsername());
        Employee one = employeeService.getOne(wrapper);

        // 3.是否存在用户名
        if (one == null) {
            return R.error("登录失败，用户名不存在");
        }

        // 4.密码比对
        if (!one.getPassword().equals(password)) {
            return R.error("用户名或密码错误");
        }

        // 5.判断状态是否为禁用 (0表禁用，1表启用)
        if (one.getStatus() == 0) {
            return R.error("账号已禁用");
        }

        // 6.登录成功，将员工ID存入Session并返回登录结果
        request.getSession().setAttribute("employee", one.getId());
        return R.success(one);
    }


    /**
     * @Author: FRSF
     * @Date: 2023/1/10 0:51
     * @Description 用户退出
     */
    @PostMapping("/logout")
    public R<String> logout(HttpServletRequest request) {
        // 清理Session中保存的当前登录员工ID
        request.getSession().removeAttribute("employee");
        return R.success("退出成功");
    }


    /**
     * @Author: FRSF
     * @Date: 2023/1/11 23:44
     * @Description 新增员工
     * @param employee: 员工信息
     * @return: top.frsf.reggie.common.R<java.lang.String>
     */
    @PostMapping
    public R<String> save(HttpServletRequest request, @RequestBody Employee employee) {
        log.info("新增员工信息：{}", employee.toString());
        // 设置初始密码123456，但需要md5加密处理
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
        employeeService.save(employee);
        return R.success("新增员工成功");
    }


    /**
     * @Author: FRSF
     * @Date: 2023/1/12 18:58
     * @Description 员工信息分页查询
     * @param page:         请求页号
     * @param pageSize:     每页大小
     * @param name:         员工姓名(条件查询，可以为空)
     * @return: top.frsf.reggie.common.R<com.baomidou.mybatisplus.extension.plugins.pagination.Page < top.frsf.reggie.entity.Employee>>
     */
    @GetMapping("/page")
    public R<Page<Employee>> page(int page, int pageSize, @RequestParam(required = false) String name) {
        log.info("分页上传信息:page={},pageSize={},name={}", page, pageSize, name);

        // 构造分页
        Page<Employee> pageInfo = new Page<>(page, pageSize);
        // 构造条件
        LambdaQueryWrapper<Employee> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(name != null, Employee::getName, name);
        wrapper.orderByDesc(Employee::getUpdateTime);
        employeeService.page(pageInfo, wrapper);
        return R.success(pageInfo);
    }

    /**
     * @Author: FRSF
     * @Date: 2023/1/12 21:19
     * @Description 修改员工信息
     * @param employee:
     * @return: top.frsf.reggie.common.R<java.lang.String>
     */
    @PutMapping
    public R<String> update(@RequestBody Employee employee) {
        log.info("update:{}", employee.toString());
        employeeService.updateById(employee);
        return R.success("员工信息修改成功");
    }


    /**
     * @Author: FRSF
     * @Date: 2023/1/12 21:21
     * @Description 根据ID查询员工信息
     * @param id: 员工ID
     * @return: top.frsf.reggie.common.R<top.frsf.reggie.entity.Employee>
     */
    @GetMapping("/{id}")
    public R<Employee> getById(@PathVariable String id) {
        log.info("通过ID查询用户信息");
        Employee byId = employeeService.getById(id);
        return byId != null ? R.success(byId) : R.error("没有对于员工信息");
    }


    /**
     * @Author: FRSF
     * @Date: 2023/1/12 21:23
     * @Description 测试用例
     * @return: java.lang.String
     */
    @GetMapping("/test")
    public String test() {
        return "TEST";
    }


}
